

#import "ABCIntroView.h"
#import "UIImageView+WebCache.h"
#import "HTFrameAlert.h"
#import "htvmainform.h"

@interface ABCIntroView () <UIScrollViewDelegate>
@property (strong, nonatomic)  UIScrollView *scrollView;
@property (strong, nonatomic)  UIPageControl *pageControl;
@property UIView *holeView;
@property UIView *circleView;
@property UIButton *doneButton;

@property (nonatomic, strong) NSOperationQueue *queue;


@end

@implementation ABCIntroView


- (NSOperationQueue *)queue {
    if (!_queue) _queue = [[NSOperationQueue alloc] init];
    return _queue;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if(self){
        
        CGRect rect = [[UIScreen mainScreen] bounds];
        CGSize size = rect.size;
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        self.scrollView.pagingEnabled = YES;
        [self addSubview:self.scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 190, self.frame.size.width, 0)];
        self.pageControl.currentPageIndicatorTintColor =[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1.0];
        //[self addSubview:self.pageControl];
        
        
        for(int i=0;i<[_picArray count];i++){
            if(i==0){
                
                NSDictionary *dict=[_picArray objectAtIndex:i];
                
                CGFloat originWidth = self.frame.size.width;
                CGFloat originHeight = self.frame.size.height;
                
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, originWidth, originHeight)];
                
                UIView *backview=[[UIView alloc]initWithFrame:CGRectMake(30, 80,originWidth-60 , originHeight-150)];
                backview.layer.borderColor=[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0].CGColor;
                backview.layer.borderWidth=0.6;
                [view addSubview:backview];
                
                
                UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(originWidth/2-50,180, 100, 100)];
                [imageview setClipsToBounds:YES];
                imageview.contentMode=UIViewContentModeScaleAspectFill;
                imageview.accessibilityLabel=[dict objectForKey:@"picPath"];
                imageview.layer.cornerRadius=50;
                [self downloadImg:imageview];
                [view addSubview:imageview];
                
                UILabel *lbTitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, originWidth, 20)];
                lbTitle.text=[dict objectForKey:@"title"];
                lbTitle.textColor=[UIColor blackColor];
                lbTitle.font=[UIFont fontWithName:@"Arial" size:18.0];
                lbTitle.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbTitle];
                
                UILabel *lbDatestr=[[UILabel alloc]initWithFrame:CGRectMake(0, 125, originWidth, 20)];
                lbDatestr.text=[dict objectForKey:@"datestr"];
                lbDatestr.textColor=[UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
                lbDatestr.font=[UIFont fontWithName:@"Arial" size:12.0];
                lbDatestr.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbDatestr];
                
                
                UILabel *lbName=[[UILabel alloc]initWithFrame:CGRectMake(0, 300, originWidth, 25)];
                lbName.text=[dict objectForKey:@"name"];
                lbName.textColor=[UIColor blackColor];
                lbName.font=[UIFont fontWithName:@"Arial" size:22.0];
                lbName.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbName];
                
                UILabel *lbName2=[[UILabel alloc]initWithFrame:CGRectMake(0, 330, originWidth, 20)];
                lbName2.text=[dict objectForKey:@"name2"];
                lbName2.textColor=[UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
                lbName2.font=[UIFont fontWithName:@"Arial" size:12.0];
                lbName2.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbName2];
                
                
                
                UILabel *lbFoot=[[UILabel alloc]initWithFrame:CGRectMake(0, originHeight-120, originWidth, 20)];
                lbFoot.text=[dict objectForKey:@"foot"];
                lbFoot.textColor=[UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
                lbFoot.font=[UIFont fontWithName:@"Arial" size:14.0];
                lbFoot.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbFoot];
                
                
                UIView *rightView=[[UIView alloc]initWithFrame:CGRectMake(originWidth-30, 100, 30, originHeight-200)];
                rightView.layer.borderColor=[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0].CGColor;
                rightView.layer.borderWidth=0.6;
                //[view addSubview:rightView];
                
                
                
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
                tapGesture.accessibilityLabel=[dict objectForKey:@"id"];
                [view addGestureRecognizer:tapGesture];
                
                
                UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:backview.bounds];
                backview.layer.masksToBounds = NO;
                backview.layer.shadowColor = [UIColor blackColor].CGColor;
                backview.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
                backview.layer.shadowOpacity = 0.5f;
                backview.layer.shadowPath = shadowPath.CGPath;
                
                backview.backgroundColor=[UIColor whiteColor];
                
                
                
                
                
                
                self.scrollView.delegate = self;
                [self.scrollView addSubview:view];
            }
            else{
                
                NSDictionary *dict=[_picArray objectAtIndex:i];
                
                CGFloat originWidth = self.frame.size.width;
                CGFloat originHeight = self.frame.size.height;
                
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(originWidth*i, 0, originWidth, originHeight)];
                
                
                UIView *backview=[[UIView alloc]initWithFrame:CGRectMake(30, 80,originWidth-60 , originHeight-150)];
                backview.layer.borderColor=[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0].CGColor;
                backview.layer.borderWidth=0.6;
                [view addSubview:backview];
                

                
                UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(originWidth/2-50,180, 100, 100)];
                [imageview setClipsToBounds:YES];
                imageview.contentMode=UIViewContentModeScaleAspectFill;
                imageview.accessibilityLabel=[dict objectForKey:@"picPath"];
                imageview.layer.cornerRadius=50;
                [self downloadImg:imageview];
                [view addSubview:imageview];
                
                
                UILabel *lbTitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, originWidth, 20)];
                lbTitle.text=[dict objectForKey:@"title"];
                lbTitle.textColor=[UIColor blackColor];
                lbTitle.font=[UIFont fontWithName:@"Arial" size:18.0];
                lbTitle.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbTitle];
                
                UILabel *lbDatestr=[[UILabel alloc]initWithFrame:CGRectMake(0, 125, originWidth, 20)];
                lbDatestr.text=[dict objectForKey:@"datestr"];
                lbDatestr.textColor=[UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
                lbDatestr.font=[UIFont fontWithName:@"Arial" size:12.0];
                lbDatestr.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbDatestr];
                
                
                UILabel *lbName=[[UILabel alloc]initWithFrame:CGRectMake(0, 300, originWidth, 25)];
                lbName.text=[dict objectForKey:@"name"];
                lbName.textColor=[UIColor blackColor];
                lbName.font=[UIFont fontWithName:@"Arial" size:22.0];
                lbName.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbName];
                
                UILabel *lbName2=[[UILabel alloc]initWithFrame:CGRectMake(0, 330, originWidth, 20)];
                lbName2.text=[dict objectForKey:@"name2"];
                lbName2.textColor=[UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
                lbName2.font=[UIFont fontWithName:@"Arial" size:12.0];
                lbName2.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbName2];
                
                
                
                UILabel *lbFoot=[[UILabel alloc]initWithFrame:CGRectMake(0, originHeight-120, originWidth, 20)];
                lbFoot.text=[dict objectForKey:@"foot"];
                lbFoot.textColor=[UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
                lbFoot.font=[UIFont fontWithName:@"Arial" size:14.0];
                lbFoot.textAlignment=UITextAlignmentCenter;
                [view addSubview:lbFoot];
                
                
                UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, 30, originHeight-200)];
                leftView.layer.borderColor=[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0].CGColor;
                leftView.layer.borderWidth=0.6;
                //[view addSubview:leftView];
                
                
                if(i<[_picArray count]-1){
                    UIView *rightView=[[UIView alloc]initWithFrame:CGRectMake(originWidth-30, 100, 30, originHeight-200)];
                    rightView.layer.borderColor=[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0].CGColor;
                    rightView.layer.borderWidth=0.6;
                    //[view addSubview:rightView];
                    
                }
                
                
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
                tapGesture.accessibilityLabel=[dict objectForKey:@"id"];
                [view addGestureRecognizer:tapGesture];
                
                UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:backview.bounds];
                backview.layer.masksToBounds = NO;
                backview.layer.shadowColor = [UIColor blackColor].CGColor;
                backview.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
                backview.layer.shadowOpacity = 0.5f;
                backview.layer.shadowPath = shadowPath.CGPath;
                
                backview.backgroundColor=[UIColor whiteColor];
               
                
                
                self.scrollView.delegate = self;
                [self.scrollView addSubview:view];            }
            
        }
        self.pageControl.numberOfPages =[_picArray count];// [_picArray count];
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width*[_picArray count], self.frame.size.height);

        CGPoint scrollPoint = CGPointMake(0, 0);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
    return self;
}


-(void)Actiondo:(UITapGestureRecognizer *)sender{
    
    NSString *info=sender.accessibilityLabel;
    [self.delegate onDoneButtonPressed:info];

}




/**始终记住, 通过模型来修改显示. 而不要试图直接修改显示*/
- (void)downloadImg:(UIImageView *)theImageView {
    NSString *imageName=[[NSString alloc]initWithFormat:@"%@?imageView2/2/w/600",theImageView.accessibilityLabel];
    NSURL *imagePath = [NSURL URLWithString:imageName];
    [self.queue addOperationWithBlock: ^{
        //在主线程中更新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
            
            [theImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"maiya"]];
            
            
            
        }];
    }];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat pageWidth = CGRectGetWidth(self.bounds);
    CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
    self.pageControl.currentPage = roundf(pageFraction);
    
}




- (void)setPicArray:(NSMutableArray*)picArray {
    _picArray = picArray;
   
}

- (void)setClickFlag:(int)clickFlag{
    _clickFlag = clickFlag;
    
}

- (void)setFitFlag:(int)fitFlag{
    _fitFlag = fitFlag;
    
}

- (void)setIdArray:(NSMutableArray *)idArray{
    _idArray = idArray;
    [self initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
}

@end//